//
//  DirectionalQuestionViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/4.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "DirectionalQuestionViewController.h"
#import "IncumbentQuestionModel.h"
#import "FilterButton.h"
#import "MoreAlertVC.h"

@interface DirectionalQuestionViewController ()<UITextViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) IncumbentQuestionModel *selectModel;
@property (nonatomic, strong) MoreAlertVC *moreAlertVC;

@end

@implementation DirectionalQuestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    // 获取在职者问答服务列表
    [self loadIncumbentQuestions];
}

#pragma mark - 导航栏模块

- (void)setupNav {
    self.navigationItem.title = [NSString stringWithFormat:@"向%@提问", self.workmateModel.nickName];
    
    UIButton *tipB = [[UIButton alloc] init];
    [tipB setImage:[UIImage imageNamed:@"ic_problem"] forState:UIControlStateNormal];
    [tipB setMj_size:tipB.currentImage.size];
    [tipB addTarget:self action:@selector(tipBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tipB];
    
    self.textView.placeholder = @"请描述您的问题，建议越具体越好。";
}

- (void)tipBtnClick {
    _moreAlertVC = [[MoreAlertVC alloc] initWithIndex:2];
    [[UIApplication sharedApplication].keyWindow addSubview:_moreAlertVC.view];
}


/** 获取在职者问答服务列表 */
- (void)loadIncumbentQuestions {
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest getIncumbentQuestionsByUserId:self.workmateModel.userId result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"获取在职者问答服务列表 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [IncumbentQuestionModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
            [weakSelf.dataArray removeAllObjects];
            [weakSelf.dataArray addObjectsFromArray:datas];
            [weakSelf resetSkillView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetSkillView {
    for (UIView *con in self.skillView.subviews) {
        if ([con isKindOfClass:[FilterButton class]]) {
            [con removeFromSuperview];
        }
    }
    
    CGFloat btnY = 45;
    CGFloat btnX = 15;
    CGFloat btnH = 27;
    CGFloat skillViewHeight = 45 + 27 + 10;
    
    for (NSInteger i = 0; i < self.dataArray.count; i++) {
        IncumbentQuestionModel *model = [self.dataArray objectAtIndex:i];
        
        NSString *btnTitle = [NSString stringWithFormat:@"￥%@ %@", model.price, model.skillName];
        CGFloat btnW = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        
        if (btnX + btnW + 15 > kScreenWidth) {
            btnX = 15;
            btnY += 37;
            skillViewHeight += 37;
        }
        
        FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
        btn.tag = i;
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(skillBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.skillView addSubview:btn];
        
        btnX += btnW + 10;
        
        if (i == 0) {
            btn.selected = YES;
            self.selectModel = model;
        } else {
            btn.selected = NO;
        }
    }
    self.skillViewHeight.constant = skillViewHeight;
}

- (void)skillBtnClick:(FilterButton *)btn {
    if (btn.selected) {
        return;
    } else {
        btn.selected = YES;
        self.selectModel = [self.dataArray objectAtIndex:btn.tag];
        
        for (UIControl *cont in self.skillView.subviews) {
            if ([cont isKindOfClass:[FilterButton class]]) {
                if (cont.tag != btn.tag) {
                    FilterButton *everyB = (FilterButton *)cont;
                    everyB.selected = NO;
                }
            }
        }
    }
}

#pragma mark - 匿名发布按钮点击事件

- (IBAction)releaseExtBtnClick:(id)sender {
    self.releaseExtBtn.selected = !self.releaseExtBtn.selected;
    
    self.releaseBtn.selected = self.releaseExtBtn.selected;
    
    if (self.releaseExtBtn.selected) {
        self.releaseLabel.textColor = UIColorFromRGB(0x36CDDB);
    } else {
        self.releaseLabel.textColor = UIColorFromRGB(0xA5B9C2);
    }
}

#pragma mark - 底部提问按钮点击事件

- (IBAction)questionBtnClick:(id)sender {
    if (self.textView.text.length == 0) {
        [CommonUtils showToast:@"请描述您的问题"];
        return;
    }
    if (self.selectModel == nil) {
        [CommonUtils showToast:@"该用户还没有设置技能"];
        return;
    }
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    NSString *companyId = self.companyId && self.companyId.length > 0 ? self.companyId : @"";
    NSString *price = self.selectModel.price && self.selectModel.price.length > 0 ? self.selectModel.price : @"0";
    [SendRequest releaseQuestionWithRelatedId:self.selectModel.incumbentQuestionId andQuestionType:@"QUESTION_TYPE_DESIGNATED" andContent:self.textView.text andBonus:price andIsAnonymous:self.releaseBtn.selected ? @"YES" : @"NO" andCompanyId:companyId result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {

            [ [CommonUtils sharedCommonUtils] payWithType:@"GOODS_TYPE_WD" relatedId:[NSString stringWithFormat:@"%@",result[@"data"][@"questionId"]] result:^(NSDictionary *result, BOOL paySuccess) {
                [self hideProgress:self.view animated:YES];
                if (paySuccess) {
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                    [CommonUtils showToast:@"提问成功"];
                }else{
//                    [CommonUtils showToast:[result objectForKey:@"msg"]];
                }
            }];
            
            
        } else {
            [self hideProgress:self.view animated:YES];
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}



#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    if (textView.markedTextRange == nil) {
        NSLog(@"text:%@", textView.text);
        if (textView.text.length > 500) {
            ALERT_VIEW(@"最多输入500个字");
            textView.text = [textView.text subString:0 length:500];
        }
        NSString *count = [NSString stringWithFormat:@"%3zd/500",textView.text.length];
        NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:count];
        //获取要调整颜色的文字位置,调整颜色
        [hintString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0X42CDD9) range:NSMakeRange(0, 3)];
        
        [hintString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0XA5B9C2) range:NSMakeRange(3, 4)];
        
        self.countL.attributedText=hintString;
    }
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}

@end
